package com.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.web.config.JWTConfig;
import com.web.params.CommentAddParams;
import com.web.model.ResponseData;

import com.web.params.newpawParams;
import com.web.service.CommentService;
import com.web.service.InfoService;

import cn.hutool.core.util.StrUtil;


@Controller
@ResponseBody // 表示该类中的方法执行结果，会自动转换成json格式
@RequestMapping("/home") 
public class HomeController {
	@Autowired
	private InfoService infoService;
	@Autowired
	private CommentService commentService;
	
	@GetMapping("/like")
	public Object LikeAdd(@RequestParam("userId") Integer userId) {
		ResponseData responseData =infoService.LikeAdd(userId);
		return responseData;
}
	@PostMapping("/change")
	public Object change(@RequestBody newpawParams params) {
		ResponseData responseData =infoService.change(params);
		return responseData;
}
	@GetMapping("/result")
	public Object Queryresults( @RequestParam("username") String username) {
				if (StrUtil.isEmptyIfStr(username)) {
			username = null;
		} else {
			username = '%' + username + '%';
		}
	ResponseData responseData =infoService.Queryresults(username);
		return responseData;
}
	@GetMapping("/detail")
	public Object Detail(@RequestParam("userId") int userId) {
		ResponseData responseData =infoService.detail(userId);
		return responseData;
}
//展示所有问题
	@GetMapping("/allpro")
	public Object Allpro() {
		ResponseData responseData =infoService.Allpro();
		return responseData;
}
	//展示个人问题
	@GetMapping("/perpro")
	public Object Perpro(@RequestHeader("Authorization")String token) {
		Integer userId= JWTConfig.getId(token);
		ResponseData responseData =infoService.Perpro(userId);
		return responseData;
}
	@PostMapping("/comment/add")
	public Object CommentAdd(@RequestHeader("Authorization")String token,@RequestBody CommentAddParams  params) {
		Integer userId= JWTConfig.getId(token);
		params.setUserid(userId);
		int result =commentService.CommentAdd(params);
		ResponseData responseData = new ResponseData();
		if(result>0) {
			responseData.success("评论成功");
		}else {
			responseData.failed("评论失败");		
		}
		return responseData;

	}
}
